package send

import (
	"github.com/beego/beego/v2/core/validation"
	"huijing_mall/logic/smslogic"
	ask "huijing_mall/shared/kernel/ask"
	"huijing_mall/shared/kernel/hmErrors"
	"huijing_mall/variable"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

// @Summary		测试接口
// @Description	测试接口
// @Accept			json
// @Produce		json
// @Success		200	{object}	echo.Response
// @Router			/public/ping [get]
func Test(c *gin.Context) {
	ask.Success(c, nil)
}

// @Summary		发送验证码接口
// @Description	发送验证码接口
// @Accept			json
// @Produce		json
// @Success		200	{object}	echo.Response
// @Router			/api/sendSms [post]
func SendSms(c *gin.Context) {

	var req ask.SendSmsRequest
	// 从请求体中解析 JSON 数据
	if err := c.ShouldBindJSON(&req); err != nil {
		// 如果解析失败，返回错误
		ask.Errors(c, http.StatusBadRequest, err)
		return
	}
	//暂时 使用beego框里面的验证器吧 ☺☺☺☺
	variable.Valid = validation.Validation{}
	if strings.Contains(req.Link, "@") {
		variable.Valid.Required(req.Link, "email").Message("手机号或邮箱不能为空")
		variable.Valid.Email(req.Link, "email").Message("邮箱格式有误")
	} else {
		variable.Valid.Required(req.Link, "mobile").Message("手机号或邮箱不能为空")
		variable.Valid.Mobile(req.Link, "mobile").Message("手机号格式有误")
	}
	if variable.Valid.HasErrors() {
		for _, v := range variable.Valid.Errors {
			ask.Errors(c, http.StatusBadRequest, hmErrors.ErrMsg(v.Message))
			return
		}
	}

	//业务逻辑模块 最终还是决定以 gin.context 为基石
	smslogic.SmsLogic(c, req.Link)
	return
}
